#include<bits/stdc++.h>
using namespace std;

string a,b;
int n;
string s1[6],s2[6];
int ans=11;

//void dfs(string s,const int step){
//	string temp=s;
//	if(s==b){
//		ans=min(ans,step);
//		//cout<<endl;
//		return;
//	}
//	if(step>=ans) {
//		//cout<<endl;
//		return;
//	}
//	size_t pos,found;
//	for(int i=0;i<n;i++){
//		pos=0;
//		//cout<<s<<' '<<s1[i]<<' '<<step<<endl;
//		if(s.find(s1[i])==std::string::npos) continue;
//		while( (found=s.find(s1[i],pos) )!=std::string::npos){
//			pos=found+1;
//			//cout<<found<<endl;		
//			dfs(s.replace(found,s1[i].length(),s2[i]),step+1);
//			s=temp;
//		}
//	}
//}

int main(){
	map<string,bool> mp;
	cin>>a>>b;
	n=0;
	while(cin>>s1[n]>>s2[n]){
		n++;
	}
	//cout<<n<<endl;
	//dfs(a,0);
	queue<string> que;
	int step=0;
	string temp,s;
	que.push(a);
	bool suc=false;
	size_t found,pos;
	while(step<10){
		int size=que.size();
		for(int j=0;j<size;j++){
			s=que.front();
			temp=s;
			que.pop();
			for(int i=0;i<n;i++){
				pos=0;
				if(s.find(s1[i])==std::string::npos) continue;
				while( (found=s.find(s1[i],pos) )!=std::string::npos){
					pos=found+1;
					string ss=s.replace(found,s1[i].length(),s2[i]);
					if(ss==b){
						cout<<step+1;
						return 0;
					}
					if(!mp[ss]) que.push(ss);
					s=temp;
				}
			}
		}
		step++;
	}
	cout<<"NO ANSWER!";
	return 0;
}
